package com.waypolice.common.quartz;

import com.waypolice.common.utils.ApplicationContextUtil;
import com.waypolice.common.utils.MathUtils;
import com.waypolice.common.utils.StringUtil;
import com.waypolice.manager.service.*;
import com.waypolice.pojo.*;
import com.waypolice.pojo.handwriting.SolrFieldVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrServerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName SynchronizeSolrAndMysqlJob
 * @Description 同步Oracle中单件表，卷表，卷内件表至solr索引库
 * @Author wr
 * @Date 2019/8/5 000515:10
 */
public class SynchronizeSolrAndMysqlJob {
    private static Logger logger = LoggerFactory.getLogger(SynchronizeSolrAndMysqlJob.class);

    /**
     * 定时同步Oracle和solr索引库
     * 目前同步单件库，卷库，卷内件库
     */
    public void synchronizeSolrAndMysqlQuartz(){
        logger.info("进入定时同步Oracle和solr索引库方法*****************************************");
        ArrayList<SolrFieldVO> listSolrVO = new ArrayList<>();
        int id=0;
        //1.查询卷表
        VolAndItemService volAndItemService = (VolAndItemService)ApplicationContextUtil.getBean(VolAndItemService.class);
        CmsJudVolumeExample cmsJudVolumeExample = new CmsJudVolumeExample();
        CmsJudVolumeExample.Criteria criteria = cmsJudVolumeExample.createCriteria();
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(0);
        integers.add(1);
        integers.add(2);
        integers.add(3);
        criteria.andVolStatusIn(integers);
        criteria.andVolArchiveStatusEqualTo(2);
        cmsJudVolumeExample.setOrderByClause("\"VOL_ID\" ASC");
        List<CmsJudVolume> cmsJudVolumes = volAndItemService.selectAllByQueryParms(cmsJudVolumeExample);

        PDFFileConversionService pdfFileConversionService = (PDFFileConversionService)ApplicationContextUtil.getBean(PDFFileConversionService.class);
        if(cmsJudVolumes.size()!=0){
            for (CmsJudVolume cmsJudVolume : cmsJudVolumes) {
                SolrFieldVO solrFieldVO = new SolrFieldVO();
                id+=1;
                solrFieldVO.setId(String.valueOf(id));
                //卷
                solrFieldVO.setTypeTable("vol");
                solrFieldVO.setArcId(cmsJudVolume.getVolId().toString());
                solrFieldVO.setTitle(cmsJudVolume.getVolTitle());
                solrFieldVO.setItemOrVolNum(cmsJudVolume.getVolCaseNum());
                solrFieldVO.setVolNo(cmsJudVolume.getVolNo());
                solrFieldVO.setAuditLeader(cmsJudVolume.getVolAuditLeader());
                /*卷无文号
                solrFieldVO.setTextNum();*/
                solrFieldVO.setArcClass(cmsJudVolume.getVolClass());
                solrFieldVO.setStorageTime(cmsJudVolume.getVolStorageTime());
                solrFieldVO.setArchiveYear(cmsJudVolume.getVolArchiveYear());
                solrFieldVO.setArchiveUnit(cmsJudVolume.getVolArchiveUnit());
                solrFieldVO.setSecretLevel(cmsJudVolume.getVolSecret());
                solrFieldVO.setVolTotal(cmsJudVolume.getVolTotal());
                solrFieldVO.setArchivePerson(cmsJudVolume.getVolArchivePerson());
               /*卷无发文日期
               solrFieldVO.setPostDate();*/
                solrFieldVO.setItemStatus(String.valueOf(cmsJudVolume.getVolStatus()));
                solrFieldVO.setWhetherPublic(String.valueOf(cmsJudVolume.getVolPublicStatus()));
                solrFieldVO.setCarrierType(cmsJudVolume.getVolCarrier());
                /*卷层次无原文
                solrFieldVO.setPdfUrl();
                solrFieldVO.setPdfContent();*/
                listSolrVO.add(solrFieldVO);
                //2.查询卷内件
                Long volId = cmsJudVolume.getVolId();
                List<CmsJudItem> cmsJudItems = volAndItemService.selectAllByVolId(volId, 1);
                for (CmsJudItem cmsJudItem : cmsJudItems) {
                    SolrFieldVO volItemVO = new SolrFieldVO();
                    id+=1;
                    volItemVO.setId(String.valueOf(id));
                    //卷内件
                    volItemVO.setTypeTable("volItem");
                    volItemVO.setArcId(String.valueOf(cmsJudItem.getItemId()));
                    volItemVO.setTitle(cmsJudItem.getItemTitle());
                    volItemVO.setItemOrVolNum(cmsJudItem.getItemStr1());
                   /*j卷内件无案卷号
                   volItemVO.setVolNo();
                   卷内件无审计组长
                    volItemVO.setAuditLeader();
                    */
                    volItemVO.setTextNum(cmsJudItem.getItemName());
                    /*卷内件无案卷类型
                    volItemVO.setArcClass();
                    volItemVO.setStorageTime();
                    volItemVO.setArchiveYear();
                    volItemVO.setArchiveUnit();
                    */
                    volItemVO.setSecretLevel(cmsJudItem.getItemSecret());
                    /*volItemVO.setVolTotal(cmsJudVolume.getVolTotal());*/
                    volItemVO.setArchivePerson(cmsJudItem.getItemRespons());
                    volItemVO.setPostDate(cmsJudItem.getItemCreateTime());
                    volItemVO.setItemStatus(String.valueOf(cmsJudItem.getItemStatus()));
                    volItemVO.setWhetherPublic(cmsJudItem.getItemStr5());
                    /*volItemVO.setCarrierType();*/
                    String itemStr2 = cmsJudItem.getItemStr2();
                    if(StringUtils.isNotBlank(itemStr2)){
                        volItemVO.setPdfUrl(itemStr2);
                        //调用原文获取接口
                        String pdfContent = pdfFileConversionService.PDFFileConversions(itemStr2);
                        logger.info("PDF中的文段********:"+pdfContent);
                        pdfContent = MathUtils.replaceBlank(pdfContent);
                        volItemVO.setPdfContent(pdfContent);
                    }
                    listSolrVO.add(volItemVO);
                }
            }
        }
        //3.查询单件
        CmsSinItemService cmsSinItemService = (CmsSinItemService)ApplicationContextUtil.getBean(CmsSinItemService.class);
        CmsSinItemExample cmsSinItemExample = new CmsSinItemExample();
        cmsSinItemExample.setOrderByClause("\"ITEM_ID\" ASC");
        CmsSinItemExample.Criteria criteria1 = cmsSinItemExample.createCriteria();
        ArrayList<Long> integers1 = new ArrayList<>();
        integers1.add(0L);
        integers1.add(1L);
        integers1.add(2L);
        integers1.add(3L);
        criteria1.andItemStatusIn(integers1);
        criteria1.andItemArcStatusEqualTo(1L);
        List<CmsSinItem> cmsSinItems = cmsSinItemService.selectAllByQueryParms(cmsSinItemExample);
        if(cmsSinItems.size()!=0){
            for (CmsSinItem cmsSinItem : cmsSinItems) {
                id+=1;
                SolrFieldVO itemVO = new SolrFieldVO();
                itemVO.setId(String.valueOf(id));
                //单件
                itemVO.setTypeTable("sinItem");
                itemVO.setArcId(String.valueOf(cmsSinItem.getItemId()));
                itemVO.setTitle(cmsSinItem.getItemTitle());
                itemVO.setItemOrVolNum(cmsSinItem.getItemStr1());
                   /*j卷内件无案卷号
                   itemVO.setVolNo();
                   卷内件无审计组长
                    itemVO.setAuditLeader();
                    */
                itemVO.setTextNum(cmsSinItem.getItemName());
                itemVO.setArcClass(cmsSinItem.getItemExp1());
                itemVO.setStorageTime(cmsSinItem.getItemNum());
                itemVO.setArchiveYear(cmsSinItem.getItemExp3());
                itemVO.setArchiveUnit(cmsSinItem.getItemExp2());
                itemVO.setSecretLevel(cmsSinItem.getItemSecret());
                /*itemVO.setVolTotal(cmsJudVolume.getVolTotal());*/
                itemVO.setArchivePerson(cmsSinItem.getItemRespons());
                itemVO.setPostDate(cmsSinItem.getItemCreateTime());
                itemVO.setItemStatus(String.valueOf(cmsSinItem.getItemStatus()));
                itemVO.setWhetherPublic(String.valueOf(cmsSinItem.getItemPublicStatus()));
                itemVO.setCarrierType(cmsSinItem.getItemCarrier());
                String itemStr2 = cmsSinItem.getItemStr2();
                if(StringUtils.isNotBlank(itemStr2)){
                    itemVO.setPdfUrl(itemStr2);
                    //调用原文获取接口
                    String pdfContent = pdfFileConversionService.PDFFileConversions(itemStr2);
                    logger.info("PDF中的文段********:"+pdfContent);
                    pdfContent = MathUtils.replaceBlank(pdfContent);
                    itemVO.setPdfContent(pdfContent);
                }
                listSolrVO.add(itemVO);
            }
        }
        if(listSolrVO.size()!=0){
            //获取solr操作service
            SolrAllTextSearchService solrAllTextSearchService = (SolrAllTextSearchService) ApplicationContextUtil.getBean(SolrAllTextSearchService.class);
            solrAllTextSearchService.saveSolr(listSolrVO);
        }
    }
}
